#!CRUSH_PERL_PATH -w
# -*-perl-*-

#  Copyright 2008 Google Inc.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

use strict;

use Date::Calc qw(Delta_DHMS);
use Getopt::Long;

my( $dfs, $start, $end, $help, $show_version );
$dfs = 'YYYY-MM-DD';
Getopt::Long::Configure( "no_ignore_case" );
&GetOptions("help" => \$help,
            "start=s" => \$start,
            "end=s" => \$end,
            "format=s" => \$dfs,
            "Version" => \$show_version,
           );

if ($show_version) {
  crush_version();
  exit(0);
}

if (! defined($start)) {
  $start = shift(@ARGV);
}
if (! defined($end)) {
  $end = shift(@ARGV);
}

if ($help || !(defined($start) && defined($end))) {
  usage();
  exit(1);
}

my @dhms = Delta_DHMS(date_parse($dfs, $start), date_parse($dfs, $end));
print abs($dhms[0]) + 1, qq(\n);
exit(0);

sub usage {
  print STDERR <<ENDUSAGE;

calculates the number of days in a range

usage: $0 [-s <date>] [-e <date>] [-f <format>] [start] [end]

  -h, --help    print this message and exit.
  -s, --start <date>  start date of range
  -e, --end <date>  end date of range
  -f, --format <format>  date format to use (default: YYYY-MM-DD)

ENDUSAGE
}

m4_include(date_parse_format.pl)
